package Gather.ImmutableGather;

import java.util.Iterator;
import java.util.List;

/**
 * ClassName:Gather002
 * Package:Gather.ImmutableGather
 * Description:
 *
 * @Author:ZhangXin
 * @Create2023/2/23-20:22
 * @Version:v1.0
 */
public class Gather002 {
    public static void main(String[] args) {
        /*创建不可变的List集合
         * "张三","李四","王五","赵六"
         * */

        //一旦创建完毕,是无法进行修改的,在下面的代码中,只能进行查询
        List<String> list = List.of("张三", "李四", "王五", "赵六");


        System.out.println(list.get(0));
        System.out.println(list.get(1));
        System.out.println(list.get(2));
        System.out.println(list.get(3));
        System.out.println("增强for遍历");
        for (String s : list) {
            System.out.println(s);
        }
        System.out.println("迭代器遍历");
        Iterator<String> it = list.iterator();
        while (it.hasNext()) {
            String str = it.next();
            System.out.println(str);
        }
        System.out.println("普通for遍历");
        for (int i = 0; i < list.size(); i++) {
            String s = list.get(i);
            System.out.println(s);
        }
        System.out.println("Lambda表达式遍历");
        list.forEach(s -> System.out.println(s));


    }
}
